package com.ukao.cashregister.dialog;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import com.mcxtzhang.commonadapter.rv.OnItemClickListener;
import com.ukao.cashregister.R;
import com.ukao.cashregister.adapter.AddressDialogAdapter;
import com.ukao.cashregister.base.BaseDialogFragment;
import com.ukao.cashregister.bean.AddressList;
import com.ukao.cashregister.eventbus.ChooseTimeEvent;
import com.ukao.cashregister.eventbus.UserInfoEvent;
import com.ukao.cashregister.listener.MyOnItemClickListener;
import com.ukao.cashregister.utils.SystemUIUtils;
import com.ukao.cashregister.widget.StateButton;

import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * 取件地址
 * Created by ukaodev on 2017/6/2.
 */

public class AddressDialogFragment extends BaseDialogFragment {

    Unbinder unbinder;
    @BindView(R.id.add_address_btn)
    StateButton addAddressBtn;
    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;
    @BindView(R.id.cancel)
    StateButton cancel;
    @BindView(R.id.submit)
    Button submit;

    private ArrayList<AddressList> addressData;
    private AddressDialogAdapter mAddressDialogAdapter;
    private AddressList currentSelect;


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            addressData = (ArrayList<AddressList>) getArguments().getSerializable("address");

        }

    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.dialog_address_fragment, container, false);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        getDialog().setCanceledOnTouchOutside(false);
        recyclerView.setLayoutManager(new LinearLayoutManager(activity));
        mAddressDialogAdapter = new AddressDialogAdapter(activity, addressData);
        recyclerView.setAdapter(mAddressDialogAdapter);
        initListener();
    }


    @Override
    protected void initListener() {
        super.initListener();
        mAddressDialogAdapter.setOnItemClickListener(mOnItemClickListener);

    }


    private OnItemClickListener mOnItemClickListener = new MyOnItemClickListener() {
        @Override
        public void onItemClick(ViewGroup viewGroup, View view, Object o, int i) {
            super.onItemClick(viewGroup, view, o, i);
            AddressList item = (AddressList) o;
            if (!item.isCheck()) {
                item.setCheck(!item.isCheck());
                List<AddressList> mData = mAddressDialogAdapter.getDatas();
                for (int j = 0; j < mData.size(); j++) {
                    if (i != j) {
                        mData.get(j).setCheck(false);
                        mData.get(j).setDef(0);
                    }
                }

                mAddressDialogAdapter.notifyDataSetChanged();
            }

        }
    };

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }


    @Override
    protected boolean useEventBus() {
        return false;
    }

    @Subscribe
    public void onEvent(UserInfoEvent event) {

    }

    @OnClick({R.id.cancel, R.id.submit, R.id.add_address_btn,R.id.close_btn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.close_btn:
                dismiss();
                break;
            case R.id.cancel:
                dismiss();
                break;
            case R.id.submit:

                for (int i = 0; i < addressData.size(); i++) {
                    if (addressData.get(i).isCheck()) {
                        currentSelect = addressData.get(i);
                    }
                }
                if (currentSelect != null) {
                    ChooseTimeEvent.postHasData(ChooseTimeEvent.Message.aad_address, currentSelect);
                    dismiss();
                }
                break;
            case R.id.add_address_btn:
                ChooseTimeEvent.postNoData(ChooseTimeEvent.Message.address_go);
                dismiss();
                break;

        }
    }


}
